//
//  DataBaseAccess.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-7.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "DataBaseAccess.h"
#import "POAPinyin.h"
#import "Imports2.h"
#import "Singleton.h"
#import "NSString+Additions.h"
#import "JSON.h"
#import "Common.h"

@implementation DataBaseAccess
//本地当前未保存的主单或者新的主单
+(MyOrderMenus *) getOrderMenuByPbCode:(NSString *)pbCode{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderMenus where pbCode='%@' and momType='0' order by momTime desc",pbCode]];
    MyOrderMenus *tempMenu = [[MyOrderMenus new] autorelease];
    if ([rs next]) {
        tempMenu.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        tempMenu.pbCode = FormatNilObject([rs stringForColumn:@"pbCode"]);
        tempMenu.ptTableNo = FormatNilObject([rs stringForColumn:@"ptTableNo"]);
        tempMenu.ptDescripts = FormatNilObject([rs stringForColumn:@"ptDescripts"]);
        tempMenu.persons = FormatNilObject([rs stringForColumn:@"persons"]);
        tempMenu.peEmpno = FormatNilObject([rs stringForColumn:@"peEmpno"]);
        tempMenu.momDescript = FormatNilObject([rs stringForColumn:@"momDescript"]);
        tempMenu.momTime = FormatNilObject([rs stringForColumn:@"momTime"]);
        tempMenu.momType = FormatNilObject([rs stringForColumn:@"momType"]); 
        tempMenu.momSendErr = FormatNilObject([rs stringForColumn:@"momSendErr"]); 
        [rs close];
        [db close]; 
    }else {
        [rs close];
        [db close]; 
        
        tempMenu.momLocalMenu = LocalMenu;
        tempMenu.pbCode = pbCode;
        tempMenu.ptTableNo = @"未开台";
        tempMenu.ptDescripts = @"未开台";
        tempMenu.persons = @"0";
        tempMenu.peEmpno = @"未登陆";
        tempMenu.momDescript = @"未描述";
        tempMenu.momTime = CurrentTime;
        tempMenu.momType = @"0"; //默认未保存
        tempMenu.momSendErr = @"0";//默认未发送
        [tempMenu insertNewMyOrderMenu];//插入新的主单信息
    }
    return tempMenu;
}

//本地未发送的主单
+(MyOrderMenus *) getOrderMenuByMomLocalMenu:(NSString *)momLocalMenu{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderMenus where momLocalMenu = '%@'",momLocalMenu]];
    MyOrderMenus *tempMenu = [[MyOrderMenus new] autorelease];
    if ([rs next]) {
        tempMenu.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        tempMenu.pbCode = FormatNilObject([rs stringForColumn:@"pbCode"]);
        tempMenu.ptTableNo = FormatNilObject([rs stringForColumn:@"ptTableNo"]);
        tempMenu.ptDescripts = FormatNilObject([rs stringForColumn:@"ptDescripts"]);
        tempMenu.persons = FormatNilObject([rs stringForColumn:@"persons"]);
        tempMenu.peEmpno = FormatNilObject([rs stringForColumn:@"peEmpno"]);
        tempMenu.momDescript = FormatNilObject([rs stringForColumn:@"momDescript"]);
        tempMenu.momTime = FormatNilObject([rs stringForColumn:@"momTime"]);
        tempMenu.momType = FormatNilObject([rs stringForColumn:@"momType"]); 
        tempMenu.momSendErr = FormatNilObject([rs stringForColumn:@"momSendErr"]); 
    }
    [rs close];
    [db close]; 
    return tempMenu;
}


#pragma mark - 查询全局数组对象
//自动将汉字转换成拼音
+(void) testPinyin {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:@"select * from pluplu order by ppCode asc"];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluPlu *temp=[PluPlu new];
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);         
//        temp.ppDescripts = FormatNilObject([rs stringForColumn:@"ppDescripts"]);
        temp.ppDescripts = FormatNilObject([rs stringForColumn:@"ppPrices"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];
    
    for (int i=0;i<[result count];i++) {
        PluPlu *temp = (PluPlu *)[result objectAtIndex:i];
//        AppSession.language = @"zh-Hans";
//        NSString *cn=JsonField(temp.ppDescripts);
//        NSLog(@"cn=%@",cn);
//        NSString *en=Trim([POAPinyin quickConvert:cn]);
//        NSLog(@"en=%@",en);
//        NSString *newPpDescripts = [NSString stringWithFormat:@"{$zh-Hans$:$%@$,$en$:$%@$}",cn,en];
//        [self Update:[NSString stringWithFormat:@"update pluplu set ppDescripts='%@' where ppCode='%@' and pmCode='%@' and psCode='%@'",newPpDescripts,temp.ppCode,temp.pmCode,temp.psCode]];
        
       // [{$ppPrice$:$188$,$ppUnits$:${#zh-Hans#:#瓶#, #en#:##}$},{$ppPrice$:$0$,$ppUnits$:${#zh-Hans#:#瓶#, #en#:##}$}]
        
        NSString *newPpPrices = [temp.ppDescripts replaceFrom:@"#zh-Hans#:#例#, #en#:##" To:@"#zh-Hans#:#例#,#en#:#case#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#瓶#, #en#:##" To:@"#zh-Hans#:#瓶#,#en#:#btl#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#盒#, #en#:##" To:@"#zh-Hans#:#盒#,#en#:#box#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#听#, #en#:##" To:@"#zh-Hans#:#听#,#en#:#tin#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#份#, #en#:##" To:@"#zh-Hans#:#份#,#en#:#case#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#位#, #en#:##" To:@"#zh-Hans#:#位#,#en#:#per#"];
        newPpPrices = [newPpPrices replaceFrom:@"#zh-Hans#:#斤#, #en#:##" To:@"#zh-Hans#:#斤#,#en#:#jin#"];
        NSString *tempSql = [NSString stringWithFormat:@"update pluplu set ppPrices='%@' where ppCode='%@' and pmCode='%@' and psCode='%@'",newPpPrices,temp.ppCode,temp.pmCode,temp.psCode];
        [self Update:tempSql];
    }
}
+(NSMutableArray *) selectPluTable:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluTable *temp=[PluTable new];
        temp.ptTableNo = FormatNilObject([rs stringForColumn:@"ptTableNo"]);
        temp.ptPcCode = FormatNilObject([rs stringForColumn:@"ptPcCode"]);
        temp.ptDescripts = FormatNilObject([rs stringForColumn:@"ptDescripts"]);        
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluEmpno:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluEmpno *temp=[PluEmpno new];
        temp.peEmpno = FormatNilObject([rs stringForColumn:@"peEmpno"]);
        temp.peEmpname = FormatNilObject([rs stringForColumn:@"peEmpname"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluSpecialSort:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluSpecialSort *temp=[PluSpecialSort new];
        temp.pssCode = FormatNilObject([rs stringForColumn:@"pssCode"]);
        temp.pssDescripts = FormatNilObject([rs stringForColumn:@"pssDescripts"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluSpecialCode:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluSpecialCode *temp=[PluSpecialCode new];
        temp.pssCode = FormatNilObject([rs stringForColumn:@"pssCode"]);
        temp.pscCode = FormatNilObject([rs stringForColumn:@"pscCode"]);
        temp.pscDescripts = FormatNilObject([rs stringForColumn:@"pscDescripts"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}

+(NSMutableArray *) selectPluBook:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluBook *temp=[PluBook new];
        temp.pbCode = FormatNilObject([rs stringForColumn:@"pbCode"]);
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.pbDescripts = FormatNilObject([rs stringForColumn:@"pbDescripts"]);
        temp.pbImage = FormatNilObject([rs stringForColumn:@"pbImage"]);
        temp.pbSeq = FormatNilObject([rs stringForColumn:@"pbSeq"]);
        temp.pbStatus = FormatNilObject([rs stringForColumn:@"pbStatus"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluMenu:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluMenu *temp=[PluMenu new];
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.pmDescripts = FormatNilObject([rs stringForColumn:@"pmDescripts"]);
        temp.pmImage = FormatNilObject([rs stringForColumn:@"pmImage"]);
        temp.pmSeq = FormatNilObject([rs stringForColumn:@"pmSeq"]);
        temp.pmStatus = FormatNilObject([rs stringForColumn:@"pmStatus"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluSort:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluSort *temp=[PluSort new];
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.psDescripts = FormatNilObject([rs stringForColumn:@"psDescripts"]);
        temp.psImage = FormatNilObject([rs stringForColumn:@"psImage"]);
        temp.psStatus = FormatNilObject([rs stringForColumn:@"psStatus"]);
        temp.psSortType = FormatNilObject([rs stringForColumn:@"psSortType"]);
        temp.psSpecialSort = FormatNilObject([rs stringForColumn:@"psSpecialSort"]);
        temp.psRemarks = FormatNilObject([rs stringForColumn:@"psRemarks"]);
        temp.psSeq = FormatNilObject([rs stringForColumn:@"psSeq"]);       
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    return result;
}
+(NSMutableArray *) selectPluPlu:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        PluPlu *temp=[PluPlu new];
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.ppTemp = FormatNilObject([rs stringForColumn:@"ppTemp"]);
        temp.ppStatus = FormatNilObject([rs stringForColumn:@"ppStatus"]);
        
        temp.ppColor = FormatNilObject([rs stringForColumn:@"ppColor"]);
        temp.ppPosition = FormatNilObject([rs stringForColumn:@"ppPosition"]);
        temp.ppHelpCode = FormatNilObject([rs stringForColumn:@"ppHelpCode"]);
        temp.ppDescripts = FormatNilObject([rs stringForColumn:@"ppDescripts"]);
        temp.ppImage = FormatNilObject([rs stringForColumn:@"ppImage"]);
        temp.ppRecommended = FormatNilObject([rs stringForColumn:@"ppRecommended"]);
        temp.ppSeqNormal = FormatNilObject([rs stringForColumn:@"ppSeqNormal"]);
        temp.ppSeqCommend = FormatNilObject([rs stringForColumn:@"ppSeqCommend"]);
        temp.ppType = FormatNilObject([rs stringForColumn:@"ppType"]);
        temp.ppFood = nil;
        temp.ppCigarete = nil;
        temp.ppDrink = nil;
        
        //初始化price数组
        NSString *tempPpPrice = [[rs stringForColumn:@"ppPrices"] replaceFrom:@"$" To:@"\""] ;
        NSArray *priceArray = [tempPpPrice JSONValue];
        NSMutableArray *tempPrices=[[NSMutableArray new] autorelease];
        for (NSDictionary *item in priceArray) {     
            PluPrice *tempPrice = [[PluPrice new] autorelease];
            tempPrice.price = DictObject(item, @"ppPrice");
            tempPrice.units = DictObject(item, @"ppUnits");
            [tempPrices addObject:tempPrice];
        }
        temp.ppPluPrices = tempPrices;
        
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close];    
    
    //实例化特殊属性
    for (int i=0;i<[result count];i++) {
        PluPlu *temp = (PluPlu *)[result objectAtIndex:i];
        if ([temp.ppType isEqualToString:@"0"]) {//初始化食品属性
            [temp updatePluFood];
        }else if([temp.ppType isEqualToString:@"1"]){//初始化香烟属性
            [temp updatePluCigarete];
        }else if([temp.ppType isEqualToString:@"2"]){//初始化酒水属性
            [temp updatePluDrink];
        }else if([temp.ppType isEqualToString:@"3"]){//实例化套餐明细数组
            [temp updatePluPkg];
        }
        //初始化myorder数组
        [temp updateMyOrderDishes];      
    }
    
    return result;
}
+(NSMutableArray *) selectMyOrderDishes:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        MyOrderDishes *temp=[MyOrderDishes new];
        temp.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.ppType = FormatNilObject([rs stringForColumn:@"ppType"]);
        temp.modDescripts = FormatNilObject([rs stringForColumn:@"modDescripts"]);
        temp.modNumber = FormatNilObject([rs stringForColumn:@"modNumber"]);
        temp.modPrice = FormatNilObject([rs stringForColumn:@"modPrice"]);
        temp.modUnits = FormatNilObject([rs stringForColumn:@"modUnits"]);
        temp.modCook = FormatNilObject([rs stringForColumn:@"modCook"]);
        temp.modPriceUpdate = FormatNilObject([rs stringForColumn:@"modPriceUpdate"]);
        temp.modTime = FormatNilObject([rs stringForColumn:@"modTime"]);
        temp.modPluPlu = nil;
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    
    MyNSLog(@"Myorder dishes count=%d",result.count);
    
    for (int i=0;i<[result count];i++) {
        MyOrderDishes *temp = (MyOrderDishes *)[result objectAtIndex:i];
        [temp updatePluPlu];
    }
    return result;
}
+(NSMutableArray *) selectMyOrderPluPkgs:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        MyOrderPluPkgs *temp=[MyOrderPluPkgs new];
        temp.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.pkgCode = FormatNilObject([rs stringForColumn:@"pkgCode"]);
        temp.pkgDescripts = FormatNilObject([rs stringForColumn:@"pkgDescripts"]);
        temp.pkgTemp = FormatNilObject([rs stringForColumn:@"pkgTemp"]);
        temp.pkgNumber = FormatNilObject([rs stringForColumn:@"pkgNumber"]);
        temp.pkgPrice = FormatNilObject([rs stringForColumn:@"pkgPrice"]);
        temp.pkgUnit = FormatNilObject([rs stringForColumn:@"pkgUnit"]);
        temp.pkgCook = FormatNilObject([rs stringForColumn:@"pkgCook"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    return result;
}
+(NSMutableArray *) selectMyOrderMenus:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        MyOrderMenus *temp=[MyOrderMenus new];
        temp.momLocalMenu = FormatNilObject([rs stringForColumn:@"momLocalMenu"]);
        temp.pbCode = FormatNilObject([rs stringForColumn:@"pbCode"]);
        temp.ptTableNo = FormatNilObject([rs stringForColumn:@"ptTableNo"]);
        temp.ptDescripts = FormatNilObject([rs stringForColumn:@"ptDescripts"]);
        temp.persons = FormatNilObject([rs stringForColumn:@"persons"]);
        temp.peEmpno = FormatNilObject([rs stringForColumn:@"peEmpno"]);
        temp.momDescript = FormatNilObject([rs stringForColumn:@"momDescript"]);
        temp.momTime = FormatNilObject([rs stringForColumn:@"momTime"]);
        temp.momType = FormatNilObject([rs stringForColumn:@"momType"]);     
        temp.momSendErr = FormatNilObject([rs stringForColumn:@"momSendErr"]); 
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    return result;
}

+(NSMutableArray *) selectRemoteMenus:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        RemoteMenus *temp=[RemoteMenus new];
        temp.rmMenu = FormatNilObject([rs stringForColumn:@"rmMenu"]);
        temp.rmTableNo = FormatNilObject([rs stringForColumn:@"rmTableNo"]);
        temp.rmTableDescripts = FormatNilObject([rs stringForColumn:@"rmTableDescripts"]);
        temp.rmDate = FormatNilObject([rs stringForColumn:@"rmDate"]);
        temp.rmPersons = FormatNilObject([rs stringForColumn:@"rmPersons"]);
        temp.rmEmpno = FormatNilObject([rs stringForColumn:@"rmEmpno"]);
        temp.rmMode = FormatNilObject([rs stringForColumn:@"rmMode"]);
        temp.rmPayCode = FormatNilObject([rs stringForColumn:@"rmPayCode"]);
        temp.rmSrv = FormatNilObject([rs stringForColumn:@"rmSrv"]);
        temp.rmDsc = FormatNilObject([rs stringForColumn:@"rmDsc"]);
        temp.rmAmount = FormatNilObject([rs stringForColumn:@"rmAmount"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    return result;
}

+(NSMutableArray *) selectRemoteDishes:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        RemoteDishes *temp=[RemoteDishes new];
        temp.rmMenu = FormatNilObject([rs stringForColumn:@"rmMenu"]);
        temp.rdInumber = FormatNilObject([rs stringForColumn:@"rdInumber"]);
        temp.rdSort = FormatNilObject([rs stringForColumn:@"rdSort"]);
        temp.rdCode = FormatNilObject([rs stringForColumn:@"rdCode"]);
        temp.rdPrintid = FormatNilObject([rs stringForColumn:@"rdPrintid"]);
        temp.rdDescripts = FormatNilObject([rs stringForColumn:@"rdDescripts"]);
        temp.rdUnits = FormatNilObject([rs stringForColumn:@"rdUnits"]);
        temp.rdPrice = FormatNilObject([rs stringForColumn:@"rdPrice"]);
        temp.rdNumber = FormatNilObject([rs stringForColumn:@"rdNumber"]);
        temp.rdSrv = FormatNilObject([rs stringForColumn:@"rdSrv"]);
        temp.rdDsc = FormatNilObject([rs stringForColumn:@"rdDsc"]);
        temp.rdAmount = FormatNilObject([rs stringForColumn:@"rdAmount"]);
        temp.rdEmpno = FormatNilObject([rs stringForColumn:@"rdEmpno"]);
        temp.rdCook = FormatNilObject([rs stringForColumn:@"rdCook"]);
        temp.rdDate = FormatNilObject([rs stringForColumn:@"rdDate"]);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    return result;
}

+(NSMutableArray *) selectOtherImages:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return nil;
    }
    
    FMResultSet *rs=[db executeQuery:sql];
    NSMutableArray *result=[[NSMutableArray new] autorelease];
    while ([rs next]) {
        OtherImage *temp=[OtherImage new];
        temp.imageType = FormatNilObject([rs stringForColumn:@"imageType"]);
        temp.imageName = FormatNilObject([rs stringForColumn:@"imageName"]);
        MyNSLog(@"iamgeType=%@,imageName=%@",temp.imageType,temp.imageName);
        [result addObject:temp];
        [temp release];
    }
    [rs close];
    [db close]; 
    return result;
}



#pragma mark - 获取实时数据
+(float) getPluBookCount{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@'",AppSession.currentOrderMenu.momLocalMenu]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}
+(float) getPluMenuCount:(NSString *)pmCode{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and pmCode='%@'",AppSession.currentOrderMenu.momLocalMenu,pmCode]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}
+(float) getPluSortCount:(NSString *)psCode{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and psCode='%@'",AppSession.currentOrderMenu.momLocalMenu,psCode]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}

+(float) getPluPluCount:(NSString *)ppCode{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@' and ppCode='%@'",AppSession.currentOrderMenu.momLocalMenu,ppCode]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}
+(float) getTotalNumber{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@'",AppSession.currentOrderMenu.momLocalMenu]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}
+(float) getTotalAmount{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    float result = 0;
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from MyOrderDishes where momLocalMenu='%@'",AppSession.currentOrderMenu.momLocalMenu]];
    while ([rs next]) {
        result += [[rs stringForColumn:@"modNumber"] floatValue] * [[rs stringForColumn:@"modPrice"] floatValue];
    }
    [rs close];
    [db close];    
    
    return result;
}
+(NSString *) getMyOrderNumber:(NSString *)sql column:(NSString *)colName{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return 0;
    }
    NSString *result = @"0.00";
    FMResultSet *rs=[db executeQuery:sql];
    while ([rs next]) {
         result = FormatNilObject([rs stringForColumn:colName]);  
    }
    [rs close];
    [db close];    
    return result;
}
+(void) Update:(NSString *)sql{
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
        return;
    }
    [db executeUpdate:sql];
    [db close]; 
}
@end
